package cn.edu.tju.cas.rest.controller;

import cn.edu.tju.cas.entity.GroupEntity;
import cn.edu.tju.cas.rest.aspect.RequiredAdmin;
import cn.edu.tju.cas.service.GroupService;
import cn.edu.tju.cas.service.UserService;
import cn.edu.tju.cas.util.BizCode;
import cn.edu.tju.cas.util.StateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by catold on 16/3/12.
 */
@Controller
@RequestMapping("group")
public class GroupController {
    @Autowired
    private GroupService groupService;

    @Autowired
    private UserService userService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public @ResponseBody Object get() {
        StateCode stateCode = StateCode.buildCode(BizCode.SUCCESS);

        stateCode.addData("groups", groupService.findAll());
        return stateCode;
    }

    @RequiredAdmin
    @RequestMapping(value = "", method = RequestMethod.POST)
    public @ResponseBody Object add(@RequestBody GroupEntity groupEntity) {
        StateCode stateCode = StateCode.buildCode(BizCode.SUCCESS);
        groupService.addGroupAndForm(groupEntity);
        return stateCode;
    }

    @RequiredAdmin
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public @ResponseBody Object delete(@PathVariable("id") Integer id) {
        StateCode stateCode = StateCode.buildCode(BizCode.SUCCESS);
        groupService.deleteById(id);
        return stateCode;
    }

    @RequiredAdmin
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public @ResponseBody Object getById(@PathVariable("id") int id) {
        StateCode stateCode = StateCode.buildCode(BizCode.SUCCESS);

        stateCode.addData("group", groupService.findById(id));
        return stateCode;
    }


}
